package org.goldfish.library_system.controller;

import org.goldfish.library_system.entity.Book;
import org.goldfish.library_system.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Optional;

@RestController
@RequestMapping("/books")
public class BookController {
    @Autowired
    private BookService bookService;

    // 获取所有图书
    @GetMapping
    public List<Book> getAllBooks() {
        return bookService.findAllBooks();
    }

    // 根据 ID 获取图书
    @GetMapping("/{id}")
    public Optional<Book> getBookById(@PathVariable Long id) {
        return bookService.findBookById(id);
    }

    // 添加或更新图书
    @PostMapping
    public Book addOrUpdateBook(@RequestBody Book book) {
        return bookService.saveBook(book);
    }

    // 删除图书
    @DeleteMapping("/{id}")
    public void deleteBook(@PathVariable Long id) {
        bookService.deleteBook(id);
    }
}